package com.app.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.ValidationException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSON;
import com.app.entity.ResidentialEquipment;
import com.app.exception.ValidateException;
import com.app.responseVO.ReturnResultResponseVO;
import com.app.responseVO.UserResponseVO;
import com.app.service.UserEquipmentService;

@Controller
@RequestMapping("/userEquipment")
public class UserEquipmentController extends BaseController{

	@Autowired
	private UserEquipmentService userEquipmentService;

    /**
     * 新增设备
     * @param residentialEquipment
     * @param response
     * @throws IOException
     */
	@RequestMapping(value = "/insertUserEquipment")
    public void insertUserEquipment(ResidentialEquipment residentialEquipment,
                           HttpServletResponse response) throws IOException {
        UserResponseVO userResponseVO = new UserResponseVO();
        //ResidentialEquipment oResidentialEquipment = JSON.parseObject(body, ResidentialEquipment.class);
        try {
        	userResponseVO = userEquipmentService.insertUserEquipment(residentialEquipment);
        } catch (ValidateException e) {
            userResponseVO.setStatue(false);
            userResponseVO.setMsg("新增设备失败");
        }
        writeJSON(response, userResponseVO);
    }

    /**
     * 更新设备
     * @param body
     * @param response
     * @throws IOException
     */
	@RequestMapping(value = "/updateUserEquipment")
	public void updateUserEquipment(ResidentialEquipment oResidentialEquipment,
			HttpServletResponse response) throws IOException {
		UserResponseVO userResponseVO = new UserResponseVO();
		//ResidentialEquipment oResidentialEquipment = JSON.parseObject(body, ResidentialEquipment.class);
		try {
			userResponseVO = userEquipmentService.updateUserEquipment(oResidentialEquipment);
		} catch (ValidateException e) {
			userResponseVO.setStatue(false);
			userResponseVO.setMsg("失败");
		}
		writeJSON(response, userResponseVO);
	}

    /**
     * 删除设备
     * @param body
     * @param response
     * @throws IOException
     */
	@RequestMapping(value = "/deleteUserEquipment")
	public void deleteUserEquipment(ResidentialEquipment oResidentialEquipment,
			HttpServletResponse response) throws IOException {
		UserResponseVO userResponseVO = new UserResponseVO();
		//ResidentialEquipment oResidentialEquipment = JSON.parseObject(body, ResidentialEquipment.class);
		try {
			userResponseVO = userEquipmentService.deleteUserEquipment(oResidentialEquipment);
		} catch (ValidateException e) {
			userResponseVO.setStatue(false);
			userResponseVO.setMsg("失败");
		}
		writeJSON(response, userResponseVO);
	}

    /**
     * 获取列表
     * @param body
     * @param currentPage
     * @param pagesize
     * @param response
     * @throws IOException
     */
	@RequestMapping("/selectUserEquipmentList")
    public void selectUserEquipmentList(ResidentialEquipment oResidentialEquipment,@RequestParam("currentPage") int currentPage,@RequestParam("pagesize") int pagesize,
                              HttpServletResponse response) throws IOException {
        ReturnResultResponseVO<ResidentialEquipment> returnResultResponseVO = new ReturnResultResponseVO<ResidentialEquipment>();
        try {
        	//ResidentialEquipment oResidentialEquipment = new ResidentialEquipment();
        	//oResidentialEquipment = JSON.parseObject(body, ResidentialEquipment.class);
            returnResultResponseVO = userEquipmentService.selectUserEquipmentList(oResidentialEquipment,currentPage, pagesize);
            returnResultResponseVO.setStatue(true);
        } catch (ValidationException e) {
            returnResultResponseVO.setStatue(false);
        }
        writeJSON(response, returnResultResponseVO);
    }
}
